package com.market.web.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.springframework.stereotype.Controller;

@Controller("fileUploadController")
public class FileUploadController {
	
	private String getBaseUrl(){
		return FacesContext.getCurrentInstance().getExternalContext().getRealPath("//upload//");
	}
	
	public void handleFileUpload(FileUploadEvent event){
		File outputFile = new File(this.getBaseUrl() + event.getFile().getFileName());
		
		try {
			FileOutputStream out = new FileOutputStream(outputFile);
			InputStream in = event.getFile().getInputstream();
			byte[] result = new byte[1024];
			int length;
			while(in.read(result)>0){
				out.write(result);
			}
			
			in.close();
			out.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
